import Cookies from 'js-cookie'

const app = {
  state: {
    sidebar: {
      opened: !+Cookies.get('sidebarStatus')
    },
    language: localStorage.getItem('language') || 'tc',
    title: localStorage.getItem('title') || '億鴻OMS管理系統',
    filename: localStorage.getItem('filename') || ''
  },
  mutations: {
    TOGGLE_SIDEBAR: state => {
      if (state.sidebar.opened) {
        Cookies.set('sidebarStatus', 1)
      } else {
        Cookies.set('sidebarStatus', 0)
      }
      state.sidebar.opened = !state.sidebar.opened
    },
    SET_LANGUAGE: (state, language) => {
      state.language = language
      localStorage.setItem('language', language);
    },
    SET_TITLE: (state, title) => {
      state.title = title
      localStorage.setItem('title', title);
    },
    SET_FILENAME: (state, filename) => {
      state.filename = filename
      localStorage.setItem('filename', filename);
    }
  },
  actions: {
    toggleSideBar({ commit }) {
      commit('TOGGLE_SIDEBAR')
    },
    setLanguage({ commit }, language) {
      commit('SET_LANGUAGE', language)
    },
    setTitle({ commit }, title) {
      commit('SET_TITLE', title)
    },
    setFileName({ commit }, filename) {
      commit('SET_FILENAME', filename)
    }
  }
}

export default app
